//
//  Image+Tool.swift
//  YuanJuBian
//
//  Created by PerhapYs on 2022/1/4.
//

import UIKit
#if canImport(Kingfisher)
import Kingfisher

class PYKFImageDownloader{
    
    /// KF下载图片
    /// - Parameters:
    ///   - imageUrl: 图片链接
    ///   - completeBlock: 完成回调
    class func downloaderKFImage(with imageUrl:String , completeBlock:@escaping ReturnValueBlock<UIImage?>){
        guard let url = URL.init(string: imageUrl) else {
            completeBlock(nil)
            return
        }
        KingfisherManager.shared.retrieveImage(with: url) { result in
            switch result {
            case .success(let imgResult):
                completeBlock(imgResult.image)
            case .failure(let _):
                completeBlock(nil)
            }
        }
    }
}

extension UIImageView{
    
    /// 显示网络图片
    /// - Parameter url: 网络图片链接
    func setKFImage_PY(imageURL:URL?){
        
        self.kf.indicatorType = .activity  //设置图片下载动画
        self.kf.setImage(with: imageURL, placeholder: nil, options: nil, completionHandler: nil)
    }
    
    func setKFImage_PY(imageLink:String){
        
        self.setKFImage_PY(imageURL: URL.init(string: imageLink))
    }
}

extension UIButton{
    
    /// 设置按钮网络图片
    /// - Parameters:
    ///   - imageLink: 网络图片链接
    ///   - state: 图片的按钮状态
    func setKFImage_PY(with imageLink:String? , state:UIControl.State){
        guard imageLink != nil else {
            return
        }
        let imageURL = URL.init(string: imageLink!)
        self.kf.setImage(with: imageURL, for: state, placeholder: nil, options: nil, progressBlock: nil, completionHandler: nil)
    }
}
#endif

#if canImport(SDWebImage)
import SDWebImage

class PYSDImageDownloader{
    
    /// SD下载图片
    /// - Parameters:
    ///   - imageUrl: 图片链接
    ///   - completeBlock: 完成回调
    class func downloaderSDImage(with imageUrl:String , completeBlock:@escaping ReturnValueBlock<UIImage?>){
        guard let url = URL.init(string: imageUrl) else {
            completeBlock(nil)
            return
        }
        
        SDWebImageDownloader.shared.downloadImage(with: url, options: .lowPriority) { receivedSize, expectedSize, targetURL in
            
        } completed: { image, data, error, finished in
            
            completeBlock(image)
        }
    }
}

extension UIImageView{
    
    /// 显示网络图片
    /// - Parameter url: 网络图片链接
    func setSDImage_PY(imageURL:URL?,placeholderImage: UIImage? = nil,completed:SDExternalCompletionBlock? = nil){
        
//        self.sd_imageIndicator 
        self.sd_setImage(with: imageURL,placeholderImage: placeholderImage,completed: completed)
    }
    func setSDImage_PY(imageLink:String?,placeholderImage: UIImage? = nil,completed:SDExternalCompletionBlock? = nil){
        
        guard let imageLink = imageLink else {
            return
        }
        self.setSDImage_PY(imageURL: URL.init(string: imageLink),placeholderImage: placeholderImage,completed: completed)
    }
    
    func setSDGifImage_PY(imageData:Data?){
        
        let image = UIImage.sd_image(withGIFData: imageData)
        self.image = image
    }
}
extension UIButton{
    
    /// 设置按钮网络图片
    /// - Parameters:
    ///   - imageLink: 网络图片链接
    ///   - state: 图片的按钮状态
    func setSDImage_PY(with imageLink:String? , state:UIControl.State , completed:SDExternalCompletionBlock? = nil){
        guard imageLink != nil else {
            return
        }
        let imageURL = URL.init(string: imageLink!)
        self.sd_setImage(with: imageURL, for: state,completed: completed)
    }
}
#endif
